package com.yss.job.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yss.common.core.utils.StringUtils;
import com.yss.job.service.ISysJobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.job.domain.SysJobLog;
import com.yss.job.mapper.SysJobLogMapper;

/**
 * 定时任务调度日志信息 服务层
 */
@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements ISysJobLogService {
    @Autowired
    private SysJobLogMapper jobLogMapper;

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
        QueryWrapper<SysJobLog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("CREATE_TIME" );
        if (null == jobLog) {
            return this.list(wrapper);
        }
        if (StringUtils.isNotEmpty(jobLog.getJobName())) {
            wrapper.like("JOB_NAME" , jobLog.getJobName());
        }
        if (StringUtils.isNotEmpty(jobLog.getJobGroup())) {
            wrapper.eq("JOB_GROUP" , jobLog.getJobGroup());
        }
        if (null != jobLog.getStatus()) {
            wrapper.like("STATUS" , jobLog.getStatus());
        }
        if (StringUtils.isNotEmpty(jobLog.getInvokeTarget())) {
            wrapper.like("INVOKE_TARGET" , jobLog.getInvokeTarget());
        }
        if (StringUtils.isNotEmpty(jobLog.getParams())) {
            if (null != jobLog.getParams().get("beginTime" ) && StringUtils.isNotEmpty(jobLog.getParams().get("beginTime" ).toString())) {
                wrapper.ge("CREATE_TIME" , jobLog.getParams().get("beginTime" ));
            }
            if (null != jobLog.getParams().get("endTime" ) && StringUtils.isNotEmpty(jobLog.getParams().get("endTime" ).toString())) {
                wrapper.le("CREATE_TIME" , jobLog.getParams().get("endTime" ));
            }
        }
        return this.list(wrapper);
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param id 调度任务日志
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(String id) {
        return this.getById(id);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        this.save(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(String[] logIds) {
        List<String> list = Arrays.asList(logIds);
        return jobLogMapper.deleteBatchIds(list);
    }

    /**
     * 删除任务日志
     *
     * @param id 调度日志ID
     */
    @Override
    public int deleteJobLogById(String id) {
        return jobLogMapper.deleteById(id);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        this.remove(new QueryWrapper<>());
    }
}
